package ve.usb.crm;

import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import ve.usb.cohesion.runtime.CohesionAction;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import ve.usb.cohesion.runtime.HibernateUtil;
import ve.usb.crm.hibernate.*;

import java.util.List;
import org.json.JSONStringer;
import ve.usb.cohesion.runtime.Pair;

/**
 * 
 */
public class AccionesCatalogo extends CohesionAction {

    /**
     * Called by Struts for the execution of action APreCatalogos.
     * 
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreCatalogos(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VCatalogos",};
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador, empleado
        if (!CohesionActor.checkActor(request, 5)) {
            return mapping.findForward(CohesionActor.SALIDA_ACTOR);
        }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            Categoria cat = (Categoria) s.createQuery("from Categoria c where c.nombre =:clas").setString("clas", "clasificacion").uniqueResult();
            List<Categoria> listaRepeOrdenada = (List<Categoria>) s.createQuery("from Categoria c where c.tipo =:clas").setLong("clas", cat.getId()).list();

            request.setAttribute("L_Categoria", listaRepeOrdenada);
            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try {
                s.close();
            } catch (Exception ex2) {
            }
        }
        return mapping.findForward(SALIDAS[salida]);
    }

    /**
     * Called by Struts for the execution of action APreCatalogo.
     * 
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreCatalogo(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VCatalogo",};
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            HttpSession sess = request.getSession();
            Long clasificacion = (Long) sess.getAttribute("clasificacion");
            if (clasificacion == null) {
                clasificacion = (Long) request.getAttribute("clasificacion");
            }
            if (clasificacion == null) {
                clasificacion = new Long(request.getParameter("clasificacion"));
            }

            List<Catalogo> cat = (List<Catalogo>) s.createQuery("from Catalogo c where c.clasificacion=:clas").
                    setLong("clas", clasificacion).list();

            request.setAttribute("L_Catalogo", cat);
            request.setAttribute("clasificacion", clasificacion);
            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try {
                s.close();
            } catch (Exception ex2) {
            }
        }
        return mapping.findForward(SALIDAS[salida]);
    }

    /**
     * Called by Struts for the execution of action APreProducto.
     * 
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreProducto(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VProducto",};
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            HttpSession sess = request.getSession();
            Long idCatalogo = (Long) request.getAttribute("idCatalogo");
            if (idCatalogo == null) {
                String tmp = request.getParameter("idCatalogo");
                idCatalogo = Long.valueOf(tmp).longValue();
            }

            Catalogo cat = (Catalogo) s.createQuery("from Catalogo where idcatalogo = :idcat").setLong("idcat", idCatalogo).uniqueResult();
            cat.getClasificacion().getNombre();
            request.setAttribute("Catalogo", cat);
            request.setAttribute("clasificacion", cat.getClasificacion().getIdCategoria());
            sess.setAttribute("idCatalogo", idCatalogo);
            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try {
                s.close();
            } catch (Exception ex2) {
            }
        }
        return mapping.findForward(SALIDAS[salida]);
    }

    /**
     * Called by Struts for the execution of action APreEditarProducto.
     * 
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreEditarProducto(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VCrearProducto",};
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            Long idCatalogo = (Long) request.getAttribute("idCatalogo");
            if (idCatalogo == null) {
                idCatalogo = new Long(request.getParameter("idCatalogo"));
            }

            HttpSession sess = request.getSession();
            FCrearProducto fFCrearProducto = new FCrearProducto();

            Catalogo cata = (Catalogo) s.load(Catalogo.class, idCatalogo);
            fFCrearProducto.setIdCatalogo(cata.getIdCatalogo());
            fFCrearProducto.setNombre(cata.getNombre());
            fFCrearProducto.setCantidad(cata.getCantidad());
            fFCrearProducto.setCodigo(cata.getCodigo());
            fFCrearProducto.setActivo(cata.getActivo());
            fFCrearProducto.setCosto(cata.getCosto());
            fFCrearProducto.setMarca(cata.getMarca());
            fFCrearProducto.setImpuesto(cata.getImpuesto());
            fFCrearProducto.setClasificacion(cata.getClasificacion().getIdCategoria());
            fFCrearProducto.setDescripcion(cata.getDescripcion());

            Categoria tipos = (Categoria) s.createQuery("from Categoria c where c.id =:cla").setLong("cla", cata.getClasificacion().getIdCategoria()).uniqueResult();

            List<Pair> list = (List<Pair>) s.createQuery("select new ve.usb.cohesion.runtime.Pair(nombre, idCategoria) from Categoria c where c.tipo.id =:cla").setLong("cla", tipos.getTipo().getId()).list();
            list.add(0, new Pair("Seleccione...", 0));
            fFCrearProducto.setListCategoria(list);

            request.setAttribute("clasificacion", tipos);
            sess.setAttribute("FCrearProducto", fFCrearProducto);
            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try {
                s.close();
            } catch (Exception ex2) {
            }
        }
        return mapping.findForward(SALIDAS[salida]);
    }

    /**
     * Called by Struts for the execution of action AEditarProducto.
     * 
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward AEditarProducto(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"APreProducto", "APreEditarProducto",};
        final int SALIDA_0 = 0;
        final int SALIDA_1 = 1;

        int salida = SALIDA_0;
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FCrearProducto fFCrearProducto = (FCrearProducto) form;
            if (fFCrearProducto.getNombre().compareTo("") == 0) {
                request.setAttribute("msg", "Debe colocar un nombre v&aacute;lido");
                salida = SALIDA_1;
            } else if (fFCrearProducto.getCantidad() < 0) {
                request.setAttribute("msg", "Debe colocar una cantidad positiva");
                salida = SALIDA_1;
            } else if (fFCrearProducto.getClasificacion() == 0) {
                request.setAttribute("msg", "Debe seleccionar una clasificaci&oacute; valida");
                salida = SALIDA_1;
            } else {
                Catalogo cata = (Catalogo) s.load(Catalogo.class, fFCrearProducto.getIdCatalogo());
                cata.setNombre(fFCrearProducto.getNombre());
                cata.setCantidad(fFCrearProducto.getCantidad());
                cata.setCodigo(fFCrearProducto.getCodigo());
                cata.setActivo(fFCrearProducto.getActivo());

                Categoria cate = (Categoria) s.load(Categoria.class, fFCrearProducto.getClasificacion());
                cata.setClasificacion(cate);

                s.save(cata);
            }
            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try {
                s.close();
            } catch (Exception ex2) {
            }
        }
        return mapping.findForward(SALIDAS[salida]);
    }

    /**
     * Called by Struts for the execution of action AEliminarProducto.
     * 
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward AEliminarProducto(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"APreCatalogo",};
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            HttpSession sess = request.getSession();
            Long idCatalogo = (Long) request.getAttribute("idcatalogo");
            if (idCatalogo == null) {
                String tmp = request.getParameter("idCatalogo");
                idCatalogo = Long.valueOf(tmp).longValue();
            }

            Catalogo catalogo = (Catalogo) s.load(Catalogo.class, idCatalogo);
            sess.setAttribute("clasificacion", catalogo.getClasificacion().getId());
            s.delete(catalogo);
            try {
                s.flush();
            } catch (Exception ex1) {
                ex1.printStackTrace();
                request.setAttribute("msg", "El producto no pudo ser eliminado.");
            }
            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try {
                s.close();
            } catch (Exception ex2) {
            }
        }
        return mapping.findForward(SALIDAS[salida]);
    }

    /**
     * Called by Struts for the execution of action APreCrearProducto.
     * 
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreCrearProducto(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VCrearProducto",};
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            HttpSession sess = request.getSession();
            FCrearProducto fFCrearProducto = new FCrearProducto();
            Long idClasificacion = (Long) request.getAttribute("clasificacion");
            if (idClasificacion == null) {
                String tmp = request.getParameter("clasificacion");
                if (tmp != null) {
                    idClasificacion = Long.valueOf(tmp).longValue();
                }
            }

            if (idClasificacion == null) {
                cargarCollectionProductos(fFCrearProducto, s);
            } else {
                Categoria tipos = (Categoria) s.createQuery("from Categoria c where c.id =:cla").setLong("cla", idClasificacion).uniqueResult();

                fFCrearProducto.setClasificacion(tipos.getIdCategoria());

                List<Pair> list = (List<Pair>) s.createQuery("select new ve.usb.cohesion.runtime.Pair(nombre, idCategoria) from Categoria c where c.tipo =:cla").setLong("cla", tipos.getTipo().getId()).list();

                list.add(0, new Pair("Seleccione...", 0));

                fFCrearProducto.setListCategoria(list);

                request.setAttribute("clasificacion", idClasificacion);
            }

            sess.setAttribute("FCrearProducto", fFCrearProducto);
            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try {
                s.close();
            } catch (Exception ex2) {
            }
        }
        return mapping.findForward(SALIDAS[salida]);
    }

    /**
     * Called by Struts for the execution of action ACrearProducto.
     * 
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward ACrearProducto(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"APreCatalogo", "VCrearProducto",};
        final int SALIDA_0 = 0;
        final int SALIDA_1 = 1;

        int salida = SALIDA_0;
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FCrearProducto fFCrearProducto = (FCrearProducto) form;
            boolean hayError = false;

            if (fFCrearProducto.getNombre().compareTo("") == 0) {
                hayError = true;
                request.setAttribute("nombreError", "<span class='ui-icon ui-icon-alert' style='float: left; margin-right: .3em;'></span>Debe colocar un nombre v&aacute;lido");
            }
            if (fFCrearProducto.getCantidad() < 0) {
                hayError = true;
                request.setAttribute("nombreError", "<span class='ui-icon ui-icon-alert' style='float: left; margin-right: .3em;'></span>Debe colocar una cantidad v&aacute;lida");
            }
            if (fFCrearProducto.getClasificacion() == 0) {
                hayError = true;
                request.setAttribute("nombreError", "<span class='ui-icon ui-icon-alert' style='float: left; margin-right: .3em;'></span>Debe colocar una clasificaci&oacute;n v&aacute;lida");
            }
            if (fFCrearProducto.getCosto() == 0.0) {
                hayError = true;
                request.setAttribute("costoError", "<span class='ui-icon ui-icon-alert' style='float: left; margin-right: .3em;'></span>Es un valor requerido.");
            }
            if (hayError) {
                cargarCollectionProductos(fFCrearProducto, s);
                salida = SALIDA_1;
            } else {
                Catalogo cat = new Catalogo();
                cat.setNombre(fFCrearProducto.getNombre());
                cat.setCantidad(fFCrearProducto.getCantidad());
                cat.setCodigo(fFCrearProducto.getCodigo());
                cat.setActivo(fFCrearProducto.getActivo());
                cat.setDescripcion(fFCrearProducto.getDescripcion());
                cat.setCosto(fFCrearProducto.getCosto());
                cat.setMarca(fFCrearProducto.getMarca());
                cat.setImpuesto(fFCrearProducto.getImpuesto());

                Categoria cate = (Categoria) s.load(Categoria.class, fFCrearProducto.getClasificacion());
                cat.setClasificacion(cate);
                request.setAttribute("clasificacion", cate.getId());

                if (fFCrearProducto.getIdCatalogo() == 0l) {
                    s.save(cat);
                } else {
                    cat.setIdCatalogo(fFCrearProducto.getIdCatalogo());
                    s.update(cat);
                }

                
            }
            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try {
                s.close();
            } catch (Exception ex2) {
            }
        }
        return mapping.findForward(SALIDAS[salida]);
    }

    /**
     * Called by Struts for the execution of action AEliminarClasificacion.
     * 
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward AEliminarClasificacion(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"APreCatalogos", "APreCatalogo",};
        final int SALIDA_0 = 0;
        final int SALIDA_1 = 1;

        int salida = SALIDA_0;
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            HttpSession sess = request.getSession();
            Long idClasificacion = (Long) request.getAttribute("clasificacion");
            if (idClasificacion == null) {
                String tmp = request.getParameter("clasificacion");
                idClasificacion = Long.valueOf(tmp).longValue();
            }

            Categoria categoria = (Categoria) s.load(Categoria.class, idClasificacion);

            List<Catalogo> tmp = (List<Catalogo>) s.createQuery("from Catalogo c where c.clasificacion =:clas").setLong("clas", categoria.getId()).list();

            if (tmp.size() > 0) {
                salida = SALIDA_1;
                request.setAttribute("msg", "<span class='ui-icon ui-icon-alert' style='float: left; margin-right: .3em;'></span>Para eliminar la clasificaci&oacute;n no debe tener ning&uacute;n producto asociado");
                request.setAttribute("categoria", categoria.getId());
            } else {
                s.delete(categoria);
            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try {
                s.close();
            } catch (Exception ex2) {
            }
        }
        return mapping.findForward(SALIDAS[salida]);
    }

    /**
     * Called by Struts for the execution of action APreCrearClasificacion.
     * 
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreCrearClasificacion(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VCrearClasificacion",};
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            HttpSession sess = request.getSession();
            FCrearCategoria fFCrearCategoria = new FCrearCategoria();
            Categoria categoria = (Categoria) s.createQuery("from Categoria c where c.nombre =:clas").setString("clas", "clasificacion").uniqueResult();

            fFCrearCategoria.setTipo(categoria.getId());
            sess.setAttribute("FCrearCategoria", fFCrearCategoria);
            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try {
                s.close();
            } catch (Exception ex2) {
            }
        }
        return mapping.findForward(SALIDAS[salida]);
    }

    /**
     * Called by Struts for the execution of action ACrearClasificacion.
     * 
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward ACrearClasificacion(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"APreCatalogos", "VCrearClasificacion",};
        final int SALIDA_0 = 0;
        final int SALIDA_1 = 1;

        int salida = SALIDA_0;
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FCrearCategoria fFCrearCategoria = (FCrearCategoria) form;
            Categoria cate = new Categoria();
            String nom = fFCrearCategoria.getNombre();
            boolean hayError = false;

            if (!nom.equals("")) {
                cate.setNombre(fFCrearCategoria.getNombre());
                Categoria cates = (Categoria) s.load(Categoria.class, fFCrearCategoria.getTipo());
                cate.setTipo(cates);
            } else {
                hayError = true;
                request.setAttribute("nombreError", "<span class='ui-icon ui-icon-alert' style='float: left; margin-right: .3em;'></span>Debe colocar un nombre v&aacute;lido");
            }

            if (hayError) {
                salida = SALIDA_1;
            } else {
                s.save(cate);

            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try {
                s.close();
            } catch (Exception ex2) {
            }
        }
        return mapping.findForward(SALIDAS[salida]);
    }

    /**
     * Called by Struts for the execution of action AMenuPrincipal.
     * 
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward AMenuPrincipal(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"APreAdmin", "APreMenuEmpleado", "APreMenuProveedor",};
        final int SALIDA_0 = 0;
        final int SALIDA_1 = 1;
        final int SALIDA_2 = 2;

        int salida = SALIDA_0;
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try {
                s.close();
            } catch (Exception ex2) {
            }
        }
        return mapping.findForward(SALIDAS[salida]);
    }

    /**
     * Called by Struts for the execution of action APreInventario.
     * 
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreInventario(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VInventario",};
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try {
                s.close();
            } catch (Exception ex2) {
            }
        }
        return mapping.findForward(SALIDAS[salida]);
    }

    /**
     * Called by Struts for the execution of action APreListaProductoaAjax.
     * 
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreListaProductoaAjax(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VListarProductoAjax",};
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            String value = request.getParameter("term");
            String consulta = "%" + value.toLowerCase() + "%";
            List<Catalogo> productos = (List<Catalogo>) s.createQuery("from Catalogo where lower(nombre) like :consulta").setString("consulta", consulta).list();

//            request.setAttribute("productos", productos);
            if (productos != null) {

                JSONStringer js = new JSONStringer();
// Begin array
                js.array();

                for (Iterator<Catalogo> it = productos.iterator(); it.hasNext();) {
                    Catalogo catalogo = it.next();

                    js.object().key("id").value(catalogo.getIdCatalogo()).
                            key("label").value(catalogo.getNombre()).key("value").value(catalogo.getNombre()).endObject();

                }

                js.endArray();
                request.setAttribute("msg", js.toString());
            }
// end array



            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try {
                s.close();
            } catch (Exception ex2) {
            }
        }
        return mapping.findForward(SALIDAS[salida]);
    }

    /**
     * Called by Struts for the execution of action APreAgregarProducto.
     * 
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreAgregarProducto(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VAgregarProducto",};
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try {
                s.close();
            } catch (Exception ex2) {
            }
        }
        return mapping.findForward(SALIDAS[salida]);
    }

    /**
     * Called by Struts for the execution of action AAgregarProducto.
     * 
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward AAgregarProducto(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VAgregarProducto",};
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try {
                s.close();
            } catch (Exception ex2) {
            }
        }
        return mapping.findForward(SALIDAS[salida]);
    }

    private void cargarCollectionProductos(FCrearProducto fFCrearProducto, Session s) {

        Categoria cat = (Categoria) s.createQuery("from Categoria c where c.nombre =:clas").setString("clas", "clasificacion").uniqueResult();

        List<Pair> list = (List<Pair>) s.createQuery("select new ve.usb.cohesion.runtime.Pair(nombre, idCategoria) from Categoria c where c.tipo =:cla").setLong("cla", cat.getId()).list();

        list.add(0, new Pair("Seleccione...", 0));
        fFCrearProducto.setListCategoria(list);
    }
}
